package application.controller
{
	import application.model.ConfigProxy;
	import application.view.ApplicationMediator;
	
	import org.puremvc.interfaces.ICommand;
	import org.puremvc.interfaces.INotification;
	import org.puremvc.patterns.command.SimpleCommand;
	
	public class StartupCommand extends SimpleCommand implements ICommand
	{
		private var main:Main;
		private var swfPath:String;
		
		override public function execute(notification:INotification):void
		{
			main = notification.getBody() as Main;
			
			definePath(main);
			
			facade.registerProxy( new ConfigProxy(swfPath) );
			
			facade.registerMediator( new ApplicationMediator(main) );
		}
		
		/**
		 * Here we find the swf path, so in case if
		 * it's in a subfolder compared to html file,
		 * it would load config file properly.
		 * 
		 * @param main app path.
		 */		
		private function definePath(main:Main):void
		{
			var url:String = main.url as String;
			
			var searchPos:Number;
			
			// Use:
			//
			// searchPos = url.lastIndexof("/");
			// swfPath = url.substr(0, searchPos);
			//
			// Instead of following lines if your flex3
			// library isn't merged into code.
			
			searchPos = url.indexOf(".swf");
			swfPath = url.substr(0, searchPos);
			
			searchPos = swfPath.lastIndexOf("/");
			swfPath = swfPath.substr(0, searchPos);
		}
	}
}